<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Games</title>
</head>

<link rel="stylesheet" type="text/css" href="../css/master.css" media="screen" />
<body>
<?php include("../db.php");

session_start();
check_inactive();
authenticate_player(); 

$db = pg_connect('host=dbsrv1 dbname=csc309g7 user=csc309g7 password=aiboid4p');


if($db)
{

$myuserid = $_SESSION['userid'];
	
?>


	
	
<fieldset>
	<legend>Games you joined</legend>
	
	
	<div >
	<form name="f" action="" method="POST">
	<table border="0">
		<tr>
			<td>Sort by:
			</td>
			<td>
			<select name="sort_by_select" onchange="javascript:document.forms['f'].submit()">
				<?php
				
				
				$sort_option_array =array("Time","Location","Sport","Creator");
				$search_option_array = array("Title","Location", "Sport","Creator");
				
				foreach($sort_option_array as $val) 
				{
    				if($sort_opt_chosen = $_POST['sort_by_select'])
					{
						if($sort_opt_chosen == $val)
							echo "<option value='$val' selected>$val</option>";
						else
						{
							echo "<option value='$val' >$val</option>";
						}
						
					}
					else
					{
						echo "<option value='$val' >$val</option>";
					}
					
				}
				
				
				 
				?>
				
			</select>
			</td>
			<td>search by: </td>
			<td>
				<select name="search_by_select">
				<?php 
				foreach($search_option_array as $val) 
				{
    				if($search_opt_chosen = $_POST['search_by_select'])
					{
						if($search_opt_chosen == $val)
							echo "<option value='$val' selected>$val</option>";
						else
						{
							echo "<option value='$val' >$val</option>";
						}
						
					}
					else
					{
						echo "<option value='$val' >$val</option>";
					}
					
				}
				
				$search_term = $_POST['search_term'];		
				
				?>
				</select>
			</td>
			<td><input type="text" name="search_term" value="<?=$search_term?>"/></td>
			<td><input type="submit" value="search" /></td>
			
		</tr>
	</table>
	</form>
	</div>
	
	<div class="content events">
			
					<dl>						
						<?php 
						
							//no need
						
						
						
						if($sort_opt_chosen)
						{
							if ($sort_opt_chosen == $sort_option_array[0])
								$sort_by = " start_date, start_time, title, sport_name,location,username ";
							elseif ($sort_opt_chosen == $sort_option_array[1])
								$sort_by = " location,start_date, start_time, title, sport_name,username ";
							elseif ($sort_opt_chosen == $sort_option_array[2])
								$sort_by = " sport_name, start_date, start_time, title, location, username ";
							elseif ($sort_opt_chosen == $sort_option_array[3])
								$sort_by = " username, start_date, start_time, title, sport_name,location ";
						}
						else
						{
							 $sort_by = " start_date, start_time, title, sport_name,location,username ";
						}
						
						
						
						if($search_opt_chosen && $search_term )
						{
							if ($search_opt_chosen == $search_option_array[0])
								$and_like = " and title ILIKE '%$search_term%'  ";
							elseif ($search_opt_chosen == $search_option_array[1])
								$and_like = " and location_name ILIKE '%$search_term%'  ";
							elseif ($search_opt_chosen == $search_option_array[2])
								$and_like = " and sport_name ILIKE '%$search_term%'  ";
							elseif ($search_opt_chosen == $search_option_array[3])
								$and_like = " and username ILIKE '%$search_term%'  ";
						}
						
						
						
						
						if($_GET['location_id'])
						{
							$location_id = $_GET['location_id'];
							
							
							$query = "select gameid, sport_name, username, start_date, start_time, location_name, title, location from
								(select * from (select * from games g left join location l on g.location = l.lid where g.location = ".$location_id.") a left join sports s on a.sid = s.sid) b
								left join people p on b.creator = p.userid where start_date > now() $and_like order by $sort_by limit $itemsPerPage offset $startFrom";
							
						}
						else
						{
							$query = "select * from (select gameid, sport_name, username, start_date, start_time, location_name, title, location from 
								(select * from (select * from games g left join location l on g.location = l.lid) a left join sports s on a.sid = s.sid) b 
								left join people p on b.creator = p.userid) x left join usergame ug where x.gameid = ug.gameid and start_date > now() $and_like order by $sort_by limit $itemsPerPage offset $startFrom";
						}
						
						
						
						
						$result = pg_query($query);
						if (pg_num_rows($result)!=0)
						{
							$ind=1;
	
							while($current_games = pg_fetch_assoc($result))
							{
								
								
								if($ind%2==0)
									$background = "";
								else 
									$background = "alt";
								
							
								echo "<dt class='".$background."'><strong>"   .  date("l", strtotime($current_games['start_date']))   . "  </strong>" . date("M d", strtotime($current_games['start_date']));
								
								if($_GET['action'] && $_GET['gameid'])
								{
									$action = $_GET['action'];
									$gameid  = $_GET['gameid'];
									if($gameid == $current_games['gameid'])
									{
										if($action == 'join')
										{
											$action='quit';
											$action_image='../img/quit.gif';									
											$width="16";
											$height="14";				
											$join_query = "insert into usergame values(". $myuserid . " , " . $current_games['gameid'].", 'default')";									
											pg_query($join_query);
											
											
										}
										else
										{
											$action='join';
											$action_image='../img/join.png';
											$width="33";
											$height="20";
											$delete_query = "delete from usergame where gameid = ".$current_games['gameid']." and userid = ". $myuserid;									
											pg_query($delete_query);
											
											
										}
										
										echo "<a  class='btn save' href='Game.php?action=" . $action . "&gameid=".$current_games['gameid']."' ><img src='".$action_image."' alt='Join this game' width='". $width."' height='".$height."' border='0'></a>
											</dt>";
									}							
									else
									{
										$usergame_query = "select * from usergame where gameid = ". $current_games['gameid'] . " and userid=". $myuserid;
										
										$usergame_result = pg_query($usergame_query);
										if (pg_num_rows($usergame_result) != 0)
										{				
										
											echo "<a  class='btn save' href='Game.php?action=quit&gameid=".$current_games['gameid']."' ><img src='../img/quit.gif' alt='Join this game' width='16' height='14' border='0'></a>
											</dt>";
										}
										else
										{
											
											echo "<a  class='btn save' href='Game.php?action=join&gameid=".$current_games['gameid']."' ><img src='../img/join.png' alt='Join this game' width='33' height='20' border='0'></a>
											</dt>";
										}		
									
									}		
									
								}						
								else
									{
										$usergame_query = "select * from usergame where gameid = ". $current_games['gameid'] . " and userid=". $myuserid;
										
										$usergame_result = pg_query($usergame_query);
										if (pg_num_rows($usergame_result) != 0)
										{				
										
											echo "<a  class='btn save' href='Game.php?action=quit&gameid=".$current_games['gameid']."' ><img src='../img/quit.gif' alt='Join this game' width='16' height='14' border='0'></a>
											</dt>";
										}
										else
										{
											
											echo "<a  class='btn save' href='Game.php?action=join&gameid=".$current_games['gameid']."' ><img src='../img/join.png' alt='Join this game' width='33' height='20' border='0'></a>
											</dt>";
										}		
									
									}	
									
								
								echo "<dd class='$background'>  
									  <h4>" .  $current_games['sport_name']  . ": <a class='event_title' href='gameDetails.php?gameid=".$current_games['gameid']."'>" .  $current_games['title'] ."</a> 									
									  " . $current_games['start_time'] . "						    
										@&nbsp;<a class='locale' href='?location_id=".$current_games['location']."'>" . $current_games['location_name'] ."</a>					              
									</h4>
									<p class='attendees'> Created by <a href='Game.php'>" . $current_games['username'] . "</a> </p>
							</dd>";
								
							$ind++;
							}
							
								?>
								
					</dl>
					<?php 
										
					
							
						}
						else//return 0 rows
						{
							
							echo "<p>You haven't joined any games yet</p>";
							
						}
					
					
						
					?>
						
						
		</div>
</fieldset>

	
<?php
	
	
	
	
	





}
else
{
	echo "no connection made";
}



?>
</body>
</html>
